package com.yami.shop.mystery.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.bean.vo.ProdRightsVo;
import com.yami.shop.mystery.api.service.ActivityPresellManager;
import com.yami.shop.mystery.common.dto.ActivityPresellDto;
import com.yami.shop.mystery.common.model.ActivityRule;
import com.yami.shop.mystery.common.service.ActivityPresellQualificationServer;
import com.yami.shop.mystery.common.service.ActivityRuleService;
import com.yami.shop.mystery.common.service.ActivityWhiteListService;
import com.yami.shop.rights.common.service.PersonRightsService;
import groovy.lang.Tuple;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wxf
 * @date 2023/4/25 19:01
 */
@Slf4j
@Service
@AllArgsConstructor
public class ActivityPresellManagerImpl implements ActivityPresellManager {

    private final PersonRightsService personRightsService;

    private final ActivityRuleService activityRuleService;

    private final ActivityWhiteListService activityWhiteListService;

    private final ActivityPresellQualificationServer activityPresellQualificationServer;

    /**
     *  校验用户是否可购买  第一位 是否可售卖  第二位 限购数  第三位 权益列表 第四位 规则的限购数 第五位 当前所处的规则是否可售卖 第六位 ； 二三四位 只有可寄售时返回值
     *
     * @param presell
     * @param userId
     * @return
     */
    @Override
    public Tuple checkUser(ActivityPresellDto presell, String userId) {

        Long maxNum = 0L; // 限购数
        Date now = new Date();
        Long ruleMaxNum = 0L; // 规则限购数
        List<ProdRightsVo> rightsVos = new ArrayList<>();
        boolean isPurchase = false;
        String ruleType = ""; // 当前所处阶段

        Tuple tuple = Tuple.tuple(false, "", null, null);
        if (StringUtils.isNotBlank(presell.getActivityRule())) {
            tuple = activityRuleService.checkActivityRule(presell, userId);
        }
        // 大众购买时间，不校验优先购和优惠购
        if (ObjectUtil.isNotEmpty(presell.getPutawayTime()) && presell.getPutawayTime().getTime() > now.getTime()) {
            if (!(boolean)tuple.get(0)) {
                return Tuple.tuple(false);
            }
            ruleType = (String) tuple.get(1);
            maxNum = (long) tuple.get(2); // 限购数
            ruleMaxNum = maxNum; // 规则限购数
            ActivityRule rule = (ActivityRule) tuple.get(3); // 规则
            if (rule.getStartTime().getTime() <= now.getTime() && rule.getEndTime().getTime() >= now.getTime()) {
                // 获取权益信息
                Long type = 1L; //默认商品
                if ("3".equals(presell.getActivityType())) {
                    type = 2L;  // 盲盒
                }
                if (activityPresellQualificationServer.getQualificationCount(presell.getSpuId(), userId, type) > 0) {
                    isPurchase = true;
                }
            }
        } else {
            if (null != presell.getPurchaseNum() && 0 != presell.getPurchaseNum()) {
                maxNum = presell.getPurchaseNum().longValue();
                ruleMaxNum = presell.getPurchaseNum().longValue(); // 规则限购数
            }
            isPurchase = true;
        }

        return Tuple.tuple(true, maxNum, rightsVos, ruleMaxNum, isPurchase, ruleType);
    }
}
